﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Net.Http;
using System.Net.Http.Formatting;

namespace EdexApiClient.api.v2
{
    public partial class EdexApiContext
    {
        private async Task<CommonApiResponse<T>> GetData<T>(string uri)
        {
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                //var apiUri = System.IO.Path.Combine(this.ApiUri, uri);
                var result = client.GetAsync(uri).Result;
                var responseText = await result.Content.ReadAsStringAsync();

                var js = new System.Web.Script.Serialization.JavaScriptSerializer();
                js.MaxJsonLength = int.MaxValue;

                if (result.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    var obj = js.Deserialize<CommonApiResponse<T>>(responseText);
                    throw new Exception(obj.Message);
                    //throw new Exception(responseText);
                }

                return js.Deserialize<CommonApiResponse<T>>(responseText);
            }
        }

        private async Task<CommonApiResponse> GetData(string uri)
        {
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                var result = client.GetAsync(uri).Result;
                var responseText = await result.Content.ReadAsStringAsync();

                var js = new System.Web.Script.Serialization.JavaScriptSerializer();
                js.MaxJsonLength = int.MaxValue;

                if (result.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    var obj = js.Deserialize<CommonApiResponse>(responseText);
                    throw new Exception(obj.Message);
                    //throw new Exception(responseText);
                }

                return js.Deserialize<CommonApiResponse>(responseText);
            }
        }

        private async Task<CommonApiResponse> Post<T>(string uri, T value)
        {
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                //var apiUri = System.IO.Path.Combine(this.ApiUri, uri);
                var result = await client.PostAsJsonAsync<T>(uri, value);
                var responseText = await result.Content.ReadAsStringAsync();

                if (result.StatusCode != System.Net.HttpStatusCode.OK &&
                    result.StatusCode != System.Net.HttpStatusCode.NoContent)
                    throw new Exception(responseText);

                var js = new System.Web.Script.Serialization.JavaScriptSerializer();
                js.MaxJsonLength = int.MaxValue;

                return js.Deserialize<CommonApiResponse>(responseText);
            }
        }

        private async Task<CommonApiResponse> Put<T>(string uri, T value)
        {
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                //var apiUri = System.IO.Path.Combine(this.ApiUri, uri);
                var result = await client.PutAsJsonAsync<T>(uri, value);
                var responseText = await result.Content.ReadAsStringAsync();

                if (result.StatusCode != System.Net.HttpStatusCode.OK &&
                    result.StatusCode != System.Net.HttpStatusCode.NoContent)
                    throw new Exception(responseText);

                var js = new System.Web.Script.Serialization.JavaScriptSerializer();
                js.MaxJsonLength = int.MaxValue;

                return js.Deserialize<CommonApiResponse>(responseText);
            }
        }

        private async Task<CommonApiResponse> Delete(string uri)
        {
            using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient())
            {
                //var apiUri = System.IO.Path.Combine(this.ApiUri, uri);
                var result = await client.DeleteAsync(uri);
                var responseText = await result.Content.ReadAsStringAsync();

                if (result.StatusCode != System.Net.HttpStatusCode.OK &&
                    result.StatusCode != System.Net.HttpStatusCode.NoContent)
                    throw new Exception(responseText);

                var js = new System.Web.Script.Serialization.JavaScriptSerializer();
                js.MaxJsonLength = int.MaxValue;

                return js.Deserialize<CommonApiResponse>(responseText);
            }
        }
    }
}
